package com.zdpx.pxframework.database.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zdpx.pxframework.core.context.SecurityContextHolder;
import com.zdpx.pxframework.core.pojo.LoginUser;
import com.zdpx.pxframework.database.config.DatabaseProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * 字段自定义填充设置
 *
 * @Description
 * @Author Cheng XiaoXiao
 * @Date 2020/11/4 10:26
 * @Version 1.0
 */
@Slf4j
public class PxMetaObjectHandler implements MetaObjectHandler {

    @Resource
    private DatabaseProperties databaseProperties;

    /**
     * 使用mp实现添加操作，这个方法执行
     *
     * @param metaObject 填充的对象类型
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        // 1.判断是否开启
        // 2.判断对象是否存在此Setter
        // 3.判断用户是否已经设置

        // 1) 逻辑删除
        if (databaseProperties.getGlobalConfig().getEnableLogicDelete()) {
            String logicDeleteField = databaseProperties.getGlobalConfig().getLogicDeleteField();
            if (metaObject.hasSetter(logicDeleteField) && Objects.isNull(this.getFieldValByName(logicDeleteField, metaObject))) {
                this.strictInsertFill(metaObject, logicDeleteField,
                        () -> Integer.parseInt(databaseProperties.getGlobalConfig().getLogicNotDeleteValue()), Integer.class);
            }
        }

        // 2) 创建时间
        if (databaseProperties.getGlobalConfig().getEnableCreateTime()) {
            String createTimeField = databaseProperties.getGlobalConfig().getCreateTimeField();
            if (metaObject.hasSetter(createTimeField) && Objects.isNull(this.getFieldValByName(createTimeField, metaObject))) {
                this.strictInsertFill(metaObject, createTimeField, Date.class, new Date());
            }
        }

        // 3) 更新时间
        if (databaseProperties.getGlobalConfig().getEnableUpdateTime()) {
            String updateTimeField = databaseProperties.getGlobalConfig().getUpdateTimeField();
            if (metaObject.hasSetter(updateTimeField) && Objects.isNull(this.getFieldValByName(updateTimeField, metaObject))) {
                this.strictInsertFill(metaObject, updateTimeField, Date.class, new Date());
            }
        }

        // 4) 乐观锁版本号
        if (databaseProperties.getGlobalConfig().getEnableVersion()) {
            String versionField = databaseProperties.getGlobalConfig().getVersionField();
            if (metaObject.hasSetter(versionField) && Objects.isNull(this.getFieldValByName(versionField, metaObject))) {
                this.strictInsertFill(metaObject, versionField, () -> 1, Integer.class);
            }
        }

        LoginUser loginUser = SecurityContextHolder.getUser();
        if (loginUser != null) {
            // 5）创建人
            if (databaseProperties.getGlobalConfig().getEnableCreator()) {
                String creatorField = databaseProperties.getGlobalConfig().getCreatorField();

                if (metaObject.hasSetter(creatorField) && Objects.isNull(this.getFieldValByName(creatorField, metaObject))) {
                    this.strictInsertFill(metaObject, creatorField, loginUser::getUserName, String.class);
                }
            }

            // 5）更新人
            if (databaseProperties.getGlobalConfig().getEnableEditor()) {
                String editorField = databaseProperties.getGlobalConfig().getEditorField();

                if (metaObject.hasSetter(editorField) && Objects.isNull(this.getFieldValByName(editorField, metaObject))) {
                    this.strictInsertFill(metaObject, editorField, loginUser::getUserName, String.class);
                }
            }
        }
    }

    /**
     * 使用mp实现修改操作，这个方法执行
     *
     * @param metaObject 填充对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        //更新时间
        if (databaseProperties.getGlobalConfig().getEnableUpdateTime()) {
            String updateTimeField = databaseProperties.getGlobalConfig().getUpdateTimeField();
            if (metaObject.hasSetter(updateTimeField) && Objects.isNull(this.getFieldValByName(updateTimeField, metaObject))) {
                this.strictUpdateFill(metaObject, updateTimeField, Date.class, new Date());
            }
        }
        //修改人
        // 5）更新人
        LoginUser loginUser = SecurityContextHolder.getUser();
        if (loginUser != null) {
            if (databaseProperties.getGlobalConfig().getEnableEditor()) {
                String editorField = databaseProperties.getGlobalConfig().getEditorField();

                if (metaObject.hasSetter(editorField) && Objects.isNull(this.getFieldValByName(editorField, metaObject))) {
                    this.strictInsertFill(metaObject, editorField, loginUser::getUserName, String.class);
                }
            }
        }
    }
}
